package cum.printer;

public interface Paper {

    public static final  String newline = "\r\n";
    /**
     * 输出一个字符到纸张
     */
    public void putInChar(char c);
    /**
     * 获取输出到纸张上的内容
     */
    public String getContent();

    class TextPaper implements Paper {
        private int charPerLine = 16;//每行字符串
        private int linePerPage = 5;//每页行数
        private  String content = "";
        private  int posX = 0;
        private int posY = 0;
        private int posP = 1;

        /**
         * 获取输出到纸张上的内容
         */
        public String getContent() {
            String ret = this.content;
            //补齐本页空行，并显示页码
            if (!(posX == 0 && posY == 0)){
                int count = linePerPage - posY;
                for (int i = 0; i < count; ++i){
                    ret += Paper.newline;
                }
                ret += "==第" + posP + "页 ==";
            }
            return ret;
        }

        /**
         * 输出一个字符到纸张
         *
         * @param c
         */
        public void putInChar(char c) {
            content +=c;
            ++posX;
            //判断是否换行
            if (posX == charPerLine){
                content += Paper.newline;
                posX = 0;
                ++posX;
            }
            //判断是否翻页
            if (posY == linePerPage){
                content += "==第" + posP + "页 ==";
                content += Paper.newline + Paper.newline;
                posY = 0;
                ++posP;
            }
        }
        public  void  setCharPerLine(int charPerLine){
            this.charPerLine = charPerLine;
        }
        //使用set方法注入每页行数
        public void  setLinePerPage(int linePerPage){
            this.linePerPage = linePerPage;
        }


    }
}
